<template>
  <div class="ivu-box">
    <Breadcrumb>
      <BreadcrumbItem>用户管理</BreadcrumbItem>
      <BreadcrumbItem>新增卡券</BreadcrumbItem>
    </Breadcrumb>
    <div class="form_box">
      <Form ref="formCustom" :model="form" :label-width="120">
        <FormItem>
          <Button type="default" @click="$router.go('-1')">返回</Button>
        </FormItem>
        <br>
        <FormItem>
          卡券名称:
          <Input v-model="formInfo.coupon_name"
                 type="text"
                 placeholder="请输入卡券名称"
                 style="width: 200px;"/>
        </FormItem>
        <FormItem>
          卡券类型:
          <Select v-model="formInfo.coupon_category_id"
                  clearable
                  style="width: 200px;">
            <Option v-for="item in couponTypeList"
                    :value="item.coupon_category_id"
                    :key="item.ROW_NUMBER">{{ item.name }}
            </Option>
          </Select>
        </FormItem>
        <FormItem>
          卡券金额:
          <Input v-model="formInfo.money"
                 type="text"
                 clearable
                 placeholder="请输入卡券金额"
                 style="width: 200px"/>
        </FormItem>
        <FormItem style="margin-left: -50px;">
          发放给(用户名称):
          <Input v-model="formInfo.user_id"
                 type="text"
                 clearable
                 placeholder="请输入用户名称"
                 style="width: 200px;"/>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="addcoupon" style="margin-left: 140px;">确定</Button>&nbsp;&nbsp;&nbsp;&nbsp;
        </FormItem>
      </Form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        username: '',
        password: '',
        role_id: null,
        manager_id: null, // 经理id 经理角色账号时 不填
        email: ''// 邮箱
      },
      selectData: null, // 权限组选中下标
      selectList: [], // 权限预览id
      show: true,
      formInfo: {
        user_id: undefined,
        coupon_id: undefined,
        coupon_name: undefined,
        coupon_category_id: '1',
        money: undefined
      }, // 表单信息
      list_: [],

      list: [],
      roleList: [],

      checkAll: true,
      role_id: null,
      data: [],
      accountList: [],
      couponTypeList: [],
      my_role_id: null // 6经理jh
    }
  },
  created () {
    this.my_role_id = JSON.parse(this.astgo.getItem(this.Pn + 'info')).role_id
    this.couponType()
  },
  methods: {
    /**
     * 卡券类型名
     * @param id
     * @returns {string}
     */
    checkCouponTypeName (id) {
      switch (id) {
        case '1':
          return '现金券'
        default:
          return ''
      }
    },
    // ---------------------- axios --------------------------
    /**
     * 卡券发放用户
     */
    addcoupon () {
      const form = this.formInfo
      form.coupon_category_name = this.checkCouponTypeName(this.formInfo.coupon_category_id)
      this.axios.post('coupon/addcoupon', form)
        .then(res => {
          this.$Message.success(res.msg)
          this.$router.go(-1)
        })
    },
    /**
     * 获取所有经理
     */
    getAccount () { // 获取所有经理
      this.axios.post('device/getAccount').then(res => {
        if (res.code === 1) {
          this.accountList = res.data
        }
      })
    },
    /**
     * 获取卡券类型列表
     */
    couponType () {
      this.axios.post('coupon/couponType')
        .then(res => {
          this.couponTypeList = res.data
        })
    },
    all () {
      this.axios.post('role/rule_list').then(res => {
        if (res.code === 1) {
          this.data = res.data
        }
      })
    },

    Submit () {
      var form = this.formInfo
      form.role_id = this.role_id
      // var id =  JSON.parse(this.astgo.getItem(this.Pn + "info")).role_id;

      console.log(form)

      this.axios.post('account/addAccount', form).then(res => {
        if (res.code === 1) {
          this.$Message.success('添加成功')
          this.$router.go(-1)
        }
      })
    },

    getlist () {
      // 如果是经理只能添加操作员

      this.axios.post('role/role_list', { status: 1 }).then(res => {
        if (res.code === 1) {
          if (this.my_role_id === 6) {
            this.role_id = 7
            this.list = []

            var arr = res.data.list

            for (var i = 0, len = arr.length; i < len; i++) {
              if (arr[i].id === 7) {
                this.list.push(arr[i])
                break
              }
            }
          } else {
            this.list = res.data.list
            this.role_id = this.list[0].id
            console.log(this.selectData)
          }

          this.selectData = this.list[0].id

          this.getRole()
        }
      })
    },

    getRole () {
      this.axios.post('role/get_role_rule', { role_id: this.role_id }).then(res => {
        if (res.code === 1) {
          this.list_ = res.data
          console.log(this.list_)
        }
      })
    },
    changeSelect (e) {
      console.log('选择', e)
      this.show = false
      this.$nextTick(() => {
        this.show = true
      })
      this.role_id = e
      this.getRole()
    }

  }

}
</script>

<style scoped>
h3 {
  line-height: 50px;
}

.subordinate {
  margin: 10px 20px;
}

.checkbox {
  margin: 5px 10px;
  min-width: 200px;
}

.check {
  margin-top: 5px;
  margin-left: 30px;
  min-width: 160px;
}

.checkboxs {
  border-bottom: 1px solid silver;
  padding-bottom: 10px;
}

.outer {
  margin: 20px 0;
  padding: 10px;
  border: 1px solid silver;
  border-radius: 5px;
}
</style>
